package com.xiaoq.sites.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.sites.entity.SitesRoutePointEntity;
import com.xiaoq.sites.repository.ISitesPointRepository;
import com.xiaoq.sites.repository.ISitesRoutePointRepository;

@Repository
public class SitesRoutePointRepositoryImpl implements ISitesRoutePointRepository {
	
	private static final Logger LOG = LoggerFactory.getLogger(SitesRoutePointRepositoryImpl.class);
	
	@Autowired
	private Datastore datastore;
	
	@Autowired
	private ISitesPointRepository sitesPointRepository;
	
	@Autowired
	private IdGenerator idGenerator;

	
	@Override
	public SitesRoutePointEntity findById(String id) {
		return datastore.get(SitesRoutePointEntity.class,id);
	}

	@Override
	public List<SitesRoutePointEntity> findSitesRoutePointByRouteId(String routeId) {
		Query<SitesRoutePointEntity> sitesRoutePointQuery = datastore.find(SitesRoutePointEntity.class);
		sitesRoutePointQuery.field("routeId").equal(routeId).order("sortNum");//按sortNum升序排序， order("-sortNum")为降序排序
		return sitesRoutePointQuery.asList();
	}
	
	@Override
	public List<SitesRoutePointEntity> findSitesRoutePointByRouteIdList(List<String> routeIdList) {
		Query<SitesRoutePointEntity> sitesRoutePointQuery = datastore.find(SitesRoutePointEntity.class);
		sitesRoutePointQuery.filter("routeId in",routeIdList).order("sortNum");
		return sitesRoutePointQuery.asList();
	}

	@Override
	public List<SitesRoutePointEntity> findSitesRoutePointBySitesPointId(String sitesPointId) {
		Query<SitesRoutePointEntity> sitesRoutePointQuery = datastore.find(SitesRoutePointEntity.class);
		sitesRoutePointQuery.field("sitesPointId").equal(sitesPointId);
		return sitesRoutePointQuery.asList();
	}

	@Override
	public boolean deleteRoutePointByRouidPointId(String routeId) {
		//先查询后删除万古不变之法则
		if(StringUtils.isNotEmpty(routeId)){
			datastore.delete(datastore.createQuery(SitesRoutePointEntity.class).field("routeId").equal(routeId));
			return true;
			
		}
		
		return false;
	}

	/**
	 * 注释掉线路上新插入一个景点的功能
	 */
	// 在线路上添加一个景点，都是在线路上可以添加相同的景点的问题，因为使用routeId和sitesPointId 没法确定一个唯一值了
	@Override
	public boolean addRoutePoint(SitesRoutePointEntity sitesRoutePoint) {
		LOG.info("添加线路景点关系" + sitesRoutePoint);
		Query<SitesRoutePointEntity> sitesRoutePontQuery = null;
		String routeId = sitesRoutePoint.getRouteId();
		sitesRoutePontQuery = datastore.find(SitesRoutePointEntity.class).field("routeId").equal(routeId)
				.order("-sortNum");
		int sortNum = 0;
		SitesRoutePointEntity sitesRoutePoints = sitesRoutePontQuery.get();
		if(sitesRoutePoints==null){
			sitesRoutePoint.setSortNum(sortNum);
			sitesRoutePoint.setId(idGenerator.nextId());
			
		}else{
			sortNum = sitesRoutePoints.getSortNum();
			sitesRoutePoint.setSortNum(sortNum + 1);
			sitesRoutePoint.setId(idGenerator.nextId());
			
		}
		
		LOG.info("最终添加的线路景点关系为" + sitesRoutePoint);
		datastore.save(sitesRoutePoint);
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean delRoutePoint(String routeId, String sitesPointId) {
		Query<SitesRoutePointEntity> sitesRoutePointQuery = datastore.createQuery(SitesRoutePointEntity.class)
				.field("routeId").equal(routeId).field("sitesPointId").equal(sitesPointId);
		SitesRoutePointEntity  sitesRoutePoint = sitesRoutePointQuery.get();
		int sortNum = sitesRoutePoint.getSortNum();
		datastore.delete(sitesRoutePointQuery);
		Query<SitesRoutePointEntity> query = datastore.find(SitesRoutePointEntity.class,"sortNum >",sortNum).field("routeId").equal(routeId);
//		List<SitesRoutePointEntity> sitesRoutePointList = query.asList();
//		for(SitesRoutePointEntity sitesRoutePointModel:sitesRoutePointList){
//			int sortNumModel = sitesRoutePointModel.getSortNum()-1;
//			String routePointId = sitesRoutePointModel.getId();
//			Query<SitesRoutePointEntity> updateQuery = datastore.createQuery(SitesRoutePointEntity.class).filter("_id", routePointId);
//			UpdateOperations<SitesRoutePointEntity> operations = datastore.createUpdateOperations(SitesRoutePointEntity.class).set("sortNum",sortNumModel);
//			datastore.update(updateQuery, operations);
//			
//		}
		UpdateOperations<SitesRoutePointEntity> routePointOprations = datastore.createUpdateOperations(SitesRoutePointEntity.class).dec("sortNum");
		datastore.update(query, routePointOprations);
		return true;
	}

	@Override
	public boolean delPointRoute(String pointId) {
		// 其实线路上删除一个景点线路上sortNum的相对大小是没有变化所以不用修改
		if (StringUtils.isNotBlank(pointId)) {
			Query<SitesRoutePointEntity> sitesRoutePointQuery = datastore.find(SitesRoutePointEntity.class)
					.field("sitesPointId").equal(pointId);
			datastore.delete(sitesRoutePointQuery);
			return true;
		}
		return false;
	}

	@Override  // 这里问题的主要原因就是routeId 没法确定一个唯一的值了
	public boolean changeRoutePoint(String routeId, Integer sortNum, Integer status) {
		Query<SitesRoutePointEntity> sitesRoutePointQuery = datastore.find(SitesRoutePointEntity.class);
		sitesRoutePointQuery.field("routeId").equal(routeId).field("sortNum").equal(sortNum);
		if(sitesRoutePointQuery==null||sitesRoutePointQuery.count()==0L){
			return false;
		}
		SitesRoutePointEntity sitesRoutePoint = sitesRoutePointQuery.get();
		if(sitesRoutePoint==null){
			return false;
		}
		sortNum = sitesRoutePoint.getSortNum();
		if(status==0){
			int sortNumc = sortNum +1;
			Query<SitesRoutePointEntity> sitesRoutePointQuerys = datastore.find(SitesRoutePointEntity.class);
			sitesRoutePointQuerys.field("routeId").equal(routeId).field("sortNum").equal(sortNumc);
			SitesRoutePointEntity sitesRoutes = null;
			sitesRoutes = sitesRoutePointQuerys.get();
			sitesRoutes.setSortNum(sortNum);
			datastore.save(sitesRoutes);
			sitesRoutePoint.setSortNum(sortNumc);
			datastore.save(sitesRoutePoint);
		}else if(status ==1){
			int sortNumc = sortNum -1;
			Query<SitesRoutePointEntity> sitesRoutePointQueryz = datastore.find(SitesRoutePointEntity.class);
			sitesRoutePointQueryz.field("routeId").equal(routeId).field("sortNum").equal(sortNumc);
			SitesRoutePointEntity sitesRoutes = null;
			sitesRoutes = sitesRoutePointQueryz.get();
			sitesRoutes.setSortNum(sortNum);
			datastore.save(sitesRoutes);
			sitesRoutePoint.setSortNum(sortNumc);
			datastore.save(sitesRoutePoint);
		}
		
		return true;
	}

	@Override
	public boolean findRoutePointByrouteIdAndPointId(String routeId, String sitesPointId) {

		Query<SitesRoutePointEntity> sitesRoutePointQueryz = datastore.find(SitesRoutePointEntity.class);
		sitesRoutePointQueryz.field("routeId").equal(routeId).field("sitesPointId").equal(sitesPointId);
		if(sitesRoutePointQueryz!=null&&sitesRoutePointQueryz.count()!=0){
			return true;
		}
		return false;
	}



	

}
